package com.liao.mapth;

import android.util.Log;

import com.amap.api.maps.model.LatLng;
import com.cocoahero.android.geojson.LineString;
import com.cocoahero.android.geojson.MultiLineString;
import com.cocoahero.android.geojson.Position;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 27353 on 2017/6/17.
 */

public class Line {
    public  Line(String url)  {


        //String jsondata = httpquest.get("http://192.168.1.108:8080/mobile/elect");
       setUrl(url);

           // Log.i("test","strat getlines");
           // String jsondata = httpquest.get("http://172.28.206.245:8080/mobile");  //从后台获取数据
   /*    String jsondata = httpquest.get("http://192.168.1.105:8080/mobile");
        JSONObject jsonObjectgeom = new JSONArray(jsondata)
                    .getJSONObject(4)
                    .getJSONObject("geom");
           // List<LineString> lines = new MultiLineString(jsonObjectgeom);  //转成线
        List<LatLng> latLngs = new ArrayList<LatLng>();
        List<Position> lp=new MultiLineString(jsonObjectgeom).getLineStrings().get(0).getPositions();
        for (Position lineString:lp){
            double lat = lineString.getLatitude()-0.002395;
            double lng = lineString.getLongitude()+0.005591;
            latLngs.add(new LatLng(lat, lng));
        }

*/
    }

    public String getLinejson()throws  JSONException,IOException{
        Httpquest httpquest = new Httpquest();
        String json = httpquest.get(url);
        return json;
    }

    public List<LatLng>[] parseLine(String jsondata) throws JSONException{
        JSONArray jsonArray = new JSONArray(jsondata);
        List<LatLng> []data;
        int length = jsonArray.length();
        data = new List[length];//<LatLng>[89];
        ///解析线并获取其中的几何文件
        for(int i=0;i<length;i++){
            JSONObject jsonObjectgeom = jsonArray.getJSONObject(i).getJSONObject("geom");
            //解析线里面的点
            data[i]=new ArrayList<LatLng>();
            List<Position> lp = new MultiLineString(jsonObjectgeom).getLineStrings().get(0).getPositions();
            for (Position lineString:lp){
                double lat = lineString.getLatitude();
                double lng = lineString.getLongitude();
                data[i].add(new LatLng(lat, lng));
            }


        }
        return data;
    }

   /* public  List<LatLng> []data;*/

    public String url;

    public void setUrl(String url) {
        this.url = url;
    }
}
